package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.constants.ArticleConstants;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.mapper.ApArticleContentMapper;

import com.zhentao.mapper.ApArticleMapper;
import com.zhentao.model.article.vos.HotArticleVo;
import com.zhentao.redis.CacheService;
import com.zhentao.service.ArticleFreemarkerService;
import com.zhentao.service.ArticleService;
import com.zhentao.schedule.file.article.dto.ArticleDto;
import com.zhentao.schedule.file.article.dto.ArticleHomeDto;
import com.zhentao.schedule.file.article.pojo.ApArticle;
import com.zhentao.schedule.file.article.pojo.ApArticleConfig;
import com.zhentao.schedule.file.article.pojo.ApArticleContent;

import com.zhentao.schedule.model.common.dtos.ResponseResult;
import com.zhentao.schedule.model.common.enums.AppHttpCodeEnum;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Date 2024/4/29 15:01
 * @Author xdm
 **/

@Service
public class ArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ArticleService {

@Autowired
ArticleFreemarkerService articleFreemarkerService;
    @Autowired
    private ApArticleMapper articleMapper;
    @Resource
    private ApArticleContentMapper apArticleContentMapper;
    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;

    //设置size常量   当页面传递过来的>50的时候，默认设置的最大数是50
    private final static short MAX_PAGE_SIZE=50;

    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto dto) {
        //1.校验参数 -size
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        //如果size<50 size=size  如果size>50  size=50
        size = Math.min(size, MAX_PAGE_SIZE);
        //把size赋值给dto
        dto.setSize(size);

        //2.校验参数 -频道
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        //3.校验参数 -时间
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }

        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }

        //2.查询数据
        List<ApArticle> apArticleList= articleMapper.loadArticleList(dto,loadType);

        //3.封装结果
        return ResponseResult.okResult(apArticleList);

    }

    @Override
    @SneakyThrows
    public ResponseResult saveArticle(ArticleDto dto) {
        //1.校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断文章id是否存在，如果不存在，进行添加操作，如果存在进行修改

        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);

        if(dto.getId()==null){
            //保存文章
            articleMapper.insert(apArticle);
            //保存配置
            ApArticleConfig apArticleConfig=new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());

            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);

            apArticleConfigMapper.insert(apArticleConfig);
            //保存内容
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);

        }else{
            //进行修改操作
            //修改文章
            articleMapper.updateById(apArticle);
            //修改内容
            QueryWrapper<ApArticleContent> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleContent::getArticleId,dto.getId());
            ApArticleContent apArticleContent=apArticleContentMapper.selectOne(queryWrapper);
            apArticleContent.setContent(dto.getContent());

            apArticleContentMapper.updateById(apArticleContent);
        }
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());
        //3.返回结果 修改成功应该返回文章的id
        return ResponseResult.okResult(apArticle.getId());
    }
    @Autowired
    CacheService cacheService;

    @Override
    public ResponseResult load2(ArticleHomeDto dto, Short loadtypeLoadMore, boolean b) {
        if(b){
            String jsonStr = cacheService.get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag());
            if(StringUtils.isNotBlank(jsonStr)){
                List<HotArticleVo> hotArticleVoList = JSON.parseArray(jsonStr, HotArticleVo.class);
                ResponseResult responseResult = ResponseResult.okResult(hotArticleVoList);
                return responseResult;
            }
        }

        return load(loadtypeLoadMore,dto);
    }


}